package com.yuyh.easyadapter.glide;

import android.content.Context;
import android.graphics.Bitmap;

import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;

public class GlideCompressTransform1 extends BitmapTransformation {
    private Context mContext;
    private int mCompressWidth = 0;

    public GlideCompressTransform1(Context context) {
        super(context);
        mContext = context;
    }

    public GlideCompressTransform1(Context context, int width) {
        super(context);
        mContext = context;
        mCompressWidth = width;
    }

    protected Bitmap transform(BitmapPool pool, Bitmap toTransform,
                               int outWidth, int outHeight) {
        return upImageSize(toTransform, outWidth, outHeight);
    }

    /**
     * 相片按相框的比例动态缩放
     *
     * @param width  模板宽度
     * @param height 模板高度
     * @return
     */
    public Bitmap upImageSize(Bitmap bmp, int width, int height) {
        System.out.println("bmp:" + width);
//        Toast.makeText(mContext,"outWidth:"+width,Toast.LENGTH_SHORT).show();
        if (bmp == null) {
            return null;
        }
        if (mCompressWidth == 0) {
            return bmp;
        }
        // 计算比例
        float scaleX = (float) mCompressWidth / bmp.getWidth();// 宽的比例
        float scaleY = (float) height / bmp.getHeight();// 高的比例
        //新的宽高
        int newW = 0;
        int newH = 0;
//        if(scaleX > scaleY){
        newW = (int) (bmp.getWidth() * scaleX);
        newH = (int) (bmp.getHeight() * scaleX);
//        }else if(scaleX <= scaleY){
//            newW = (int) (bmp.getWidth() * scaleY);
//            newH = (int) (bmp.getHeight() * scaleY);
//        }
        return Bitmap.createScaledBitmap(bmp, newW, newH, true);
    }

    @Override
    public String getId() {
        return getClass().getName() + mCompressWidth;
    }
}